package db;

/**
 * Created by Administrator on 2017/7/12.
 */

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;

import entity.Cipai;



public class CipaiDatabaseHelper
{
    public static final String NuLL = "<div class=\"ci\"><blockquote><div><span class=\"lingge\">平</span><span class=\"zhong\">中</span>平仄仄仄平平，<span class=\"zhong\">平</span><span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div><span class=\"lingge\">仄</span>平平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div><span class=\"zhong\">中</span>仄平平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>仄仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div><span class=\"zhong\">中</span>仄平平仄，<span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><br class=\"empty\" /><div><span class=\"zhong\">中</span>仄<span class=\"zhong\">中</span>平<span class=\"zhong\">中</span>仄，仄<span class=\"zhong\">中</span>平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div><span class=\"lingge\">仄</span>平平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>仄仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div>仄平平、<span class=\"zhong\">中</span>平平仄，仄<span class=\"zhong\">中</span>平、<span class=\"zhong\">中</span>仄仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div>平平仄、仄平平仄，<span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div></blockquote></div>";

    private static final String TABLE_NAME = "cipai";


    public static Cipai getCipaiById(int id)
    {
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where id= " + id, null);
        ArrayList<Cipai> list = getCipaiListFromCursor(cursor);
        if (list.size() > 0)
        {
            return list.get(0);
        }
        else
        {
            return new Cipai();
        }
    }

    public static String getPingzeById(int id) {
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where id= " + id, null);
        //String pingze = cursor.getString(8);
        //System.out.println(cursor.getColumnCount());
        //String test = cursor.getString(0);
        //Log.d("tag","qi shi hai shi lai le zhe li");
        //Log.d("tag",test);
        while (cursor.moveToNext()) {
            String pingze = cursor.getString(cursor.getColumnIndex("pingze"));
            return pingze;
        }
        return null;
//        return getPingzeByIdAndSB(cursor);
    }

    private static ArrayList<Cipai> getCipaiListFromCursor(Cursor cursor)
    {
        ArrayList<Cipai> list = new ArrayList<Cipai>();
        while (cursor.moveToNext())
        {
            Cipai cipai = new Cipai();
            cipai.setId(cursor.getInt(cursor.getColumnIndex("id")));
            cipai.setAlias(cursor.getString(cursor.getColumnIndex("alias")));
            cipai.setColor_id(cursor.getInt(cursor.getColumnIndex("color_id")));
            cipai.setEnname(cursor.getString(cursor.getColumnIndex("enname")));
            cipai.setName(cursor.getString(cursor.getColumnIndex("name")));
            cipai.setParent_id(cursor.getInt(cursor.getColumnIndex("parent_id")));
            cipai.setPingze(cursor.getString(cursor.getColumnIndex("pingze")));
            cipai.setSource(cursor.getString(cursor.getColumnIndex("source")));
            cipai.setTone_type(cursor.getInt(cursor.getColumnIndex("tone_type")));
            cipai.setWordcount(cursor.getInt(cursor.getColumnIndex("wordcount")));
            list.add(cipai);
        }
        System.out.println("myth" + list.size());
        return list;
    }

}